package app.fugema.shop.main.order.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import app.fugema.shop.http.SuccessResult;
import app.fugema.shop.main.order.model.CheckOrderStatusResult;
import app.fugema.shop.main.order.model.OrderInvoiceBean;
import app.fugema.shop.main.order.model.OrderParams;
import app.fugema.shop.main.order.model.OrderShops;
import app.fugema.shop.main.order.model.PaymentResult;
import app.fugema.shop.main.order.model.PostOrderResult;
import app.fugema.shop.main.order.model.ShoppingSubmitInfo;
import app.fugema.shop.main.order.model.SubmitModelResult;
import app.fugema.shop.main.wallet.model.PayInfoResult;


import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;

import com.base.common.utils.UIUtils;
import com.base.common.viewmodel.BaseViewModel;
import app.fugema.shop.http.HttpService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class OrderConfirmVN extends BaseViewModel {

    public OrderConfirmVN(@NonNull Application application)
    {
        super(application);
    }


    /**
     *  提交购买商品信息获取提交订单信息
     * @param submitInfo
     * @param userkey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<SubmitModelResult>> getSubmitModel(ShoppingSubmitInfo submitInfo, String userkey)
    {
        //@Query("skuId") String skuId, @Query("count") int count, @Query("userkey") String userkey
        Map<String,Object>  params = new HashMap<>();
        if( UIUtils.isNotEmpty( submitInfo.getIdStr() ) )
        {
            params.put("skuId",submitInfo.getIdStr());
        }
        if( UIUtils.isNotEmpty( submitInfo.getCounts() ) )
        {
            params.put("count", Integer.parseInt( submitInfo.getCounts() ) );
        }
        if( UIUtils.isNotEmpty( submitInfo.getProducttype() ) )
        {
            params.put("producttype",submitInfo.getProducttype() );
        }
        // 优惠券
        if( UIUtils.isNotEmpty( submitInfo.getCouponIds() ) )
        {
            params.put("couponIds",submitInfo.getCouponIds() );
        }

        params.put("userkey",userkey);

        return send(HttpService.getHttpServer().getSubmitModel(params));
    }

    /**
     *  购物车提交购买商品信息获取提交订单信息
     * @param submitInfo
     * @param userkey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<SubmitModelResult>> getCartSubmitModel(ShoppingSubmitInfo submitInfo, String userkey)
    {
        //String cartItemIds,String userkey
        //cartItemIds购物车ID  shippingAddressId   收货地址ID（非必填）   string couponIds; 逗号隔开 String userkey
        Map<String,Object>  params = new HashMap<>();
        if(!submitInfo.isBuyNow() && UIUtils.isNotEmpty( submitInfo.getIdStr() ) )
        {
            params.put("cartItemIds",submitInfo.getIdStr());
        }
        //params.put("shippingAddressId",submitInfo.getProducttype() );
        // 优惠券
        if( UIUtils.isNotEmpty( submitInfo.getCouponIds() ) )
        {
            params.put("couponIds",submitInfo.getCouponIds() );
        }
        params.put("userkey",userkey);
        return send(HttpService.getHttpServer().getCartSubmitModel(params));
    }

    // 立即购买提交订单信息
    public MutableLiveData<LiveDataWrapper<PostOrderResult>> buyNowOrderSubmit(OrderParams orderInfo)
    {
        /*
        if(UIUtils.isNotEmpty( orderInfo.getShopProducts() ))
        {
            //发票信息
            OrderInvoiceBean invoiceBean = new OrderInvoiceBean();
            invoiceBean.setName("湖北汉丹机电有限公司");
            invoiceBean.setInvoicetype("2");
            invoiceBean.setInvoicecontext("公司");
            invoiceBean.setCode("914206007674415815");
            invoiceBean.setCellphone("13125043081");
            invoiceBean.setEmail("jiajucheng90@163.com");
            invoiceBean.setInvoiceTitle("湖北汉丹机电有限公司");
            invoiceBean.setInvoiceCode("914206007674415815");
        }
        */
        /*
        string skuIds;逗号隔开
        string counts; 逗号隔开
        long recieveAddressId;
        string couponIds; 逗号隔开
        int integral ;逗号隔开
        Bool isCashOnDelivery; 是否为货到付款
        Int invoiceType; 0:不需要发票 2：普通发票
        String invoiceContext; 发票内容
        String invoiceTitle; 发票抬头
        String invoiceCode; 发票税号
        decimal CapitalAmount  使用预付款金额
        string orderRemarks  订单备注（多订单情况下，订单备注用“,“分隔）
        CommonModel.OrderShop[] OrderShops;/*自提订单相关信息
        String userkey 用户凭证
        Sbyte ProductType 商品类型
        CommonModel.VirtualProductItem[] VirtualProductItems虚拟订单信息项
        VirtualProductItems集合每项包含字段：
        VirtualProductItemName:名称
        Content：内容
        VirtualProductItemType：类别
        OrderId：订单ID，默认0
        OrderItemId：订单项ID，默认0
        */

        Map<String,Object> map = new HashMap<>();
        map.put("skuids", orderInfo.getItemIds());
        map.put("counts", orderInfo.getCounts());
        map.put("virtualproductitems", JacksonUtils.transBean2Json(new ArrayList<Object>()));
        map.put("producttype", orderInfo.getProductType());
        //0
        if( UIUtils.isNotEmpty( orderInfo.getCapitalAmount() ) )
        {
            map.put("capitalamount", orderInfo.getCapitalAmount());
            // 抓包看到 还有 paypwd 参数
            //map.put("paypwd", "0");
        }
        else
        {
            map.put("capitalamount", "0");
        }

        map.put("recieveaddressid", orderInfo.getRecieveAddressId());
        //false
        map.put("iscashondelivery", orderInfo.isCashOnDelivery());
        //0
        if( UIUtils.isNotEmpty( orderInfo.getIntegral() ) )
        {
            map.put("integral", orderInfo.getIntegral());
        }
        else
        {
            map.put("integral", "0");
        }
        map.put("jsonordershops", JacksonUtils.transBean2Json(orderInfo.getShopProducts()));
        map.put("userkey", orderInfo.getUserkey());

        //立即购买 提交信息参考
        //skuids=3012_1534_2795_0&
        // counts=1&
        // virtualproductitems=[]&
        // producttype=0&
        // capitalamount=0&
        // recieveaddressid=37&
        // iscashondelivery=false&
        // integral=0&
        // jsonordershops=[{"shopId":"9","orderSkus":[{"skuId":"3012_1534_2795_0","count":"1"}],"Remark":"","deliveryType":"0","shopBrandId":0,"PostOrderInvoice":null}]&

        //带发票信息的 参数
        //skuids=3013_2561_2796_0&
        // counts=1&
        // virtualproductitems=[]&
        // producttype=0&
        // capitalamount=0&
        // recieveaddressid=44&
        // iscashondelivery=false&
        // integral=0&
        // jsonordershops=[{"shopId":"9","orderSkus":[{"skuId":"3013_2561_2796_0","count":"1"}],"Remark":"测试留言","deliveryType":"0","shopBrandId":0,"PostOrderInvoice":{"invoicetype":"2","invoicecontext":"公司","name":"湖北汉丹机电有限公司","code":"914206007674415815","cellphone":"13125043081","email":"jiajucheng90@163.com","InvoiceTitle":"湖北汉丹机电有限公司","InvoiceCode":"914206007674415815"}}]

        return send(HttpService.getHttpServer().buyNowSubmitOrder(map));
    }

    // 购物车购买提交订单信息
    public MutableLiveData<LiveDataWrapper<PostOrderResult>> cartSubmitOrder(OrderParams orderInfo)
    {
        //cartitemIds=149,148,147&capitalamount=0&recieveaddressid=37&iscashondelivery=false&integral=0&jsonordershops=[{"shopId":"9","orderSkus":[{"skuId":"2750_2553_2796_0","count":"1"},{"skuId":"2890_1526_3070_0","count":"1"},{"skuId":"2764_1523_2796_0","count":"1"}],"Remark":"","deliveryType":"0","shopBrandId":0,"PostOrderInvoice":null}]

        Map<String, Object> map = new HashMap<>();

        map.put("cartitemIds", orderInfo.getItemIds());
        if( UIUtils.isNotEmpty( orderInfo.getCapitalAmount() ) )
        {
            //使用预付款金额  0
            map.put("capitalamount",orderInfo.getCapitalAmount() );
        }
        else
        {
            //使用预付款金额  0
            map.put("capitalamount","0" );
        }

        map.put("recieveaddressid", orderInfo.getRecieveAddressId());
        //false
        map.put("iscashondelivery", orderInfo.isCashOnDelivery());
        if( UIUtils.isNotEmpty( orderInfo.getIntegral() ) )
        {
            map.put("integral", orderInfo.getIntegral());
        }
        else
        {
            map.put("integral", "0");
        }

        //map.put("couponids", orderInfo.getCounts());
        //map.put("virtualproductitems", JacksonUtils.transBean2Json(new ArrayList<Object>()));

        map.put("jsonordershops", JacksonUtils.transBean2Json(orderInfo.getShopProducts()));
        map.put("userkey", orderInfo.getUserkey());

        return send(HttpService.getHttpServer().cartSubmitOrder(map));
    }

    /**
     *  根据 订单单号信息   支付方式  获取  加签信息
     * @param orderIds
     * @param typeId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<PayInfoResult>> getPayInfoByOrderId(String orderIds, String typeId , String userKey )
    {
        return send(HttpService.getHttpServer().getPaymentListByOrder(orderIds,typeId,userKey));
    }


    /**
     *  福格玛 未知接口
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> isConBindSms(String userKey)
    {
        return send(HttpService.getHttpServer().isConBindSms(userKey));
    }

    /**
     *  查询支持的支付方式
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<PaymentResult>> enablePaymentList(String userKey)
    {
        return send(HttpService.getHttpServer().enablePaymentList(userKey));
    }

    /**
     *  检查 订单 支付状态
     * @param orderIds
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<CheckOrderStatusResult>> checkOrderPayStatus(String orderIds, String userKey)
    {
        return send(HttpService.getHttpServer().getOrderStatus(orderIds,userKey));
    }


    /**
     *  是否设置支付密码
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<SuccessResult>> isUserSetPayPwd(String userKey)
    {
        return send(HttpService.getHttpServer().isSetPayPwd(userKey));
    }

    /**
     *  验证 支付密码
     * @param pwd
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<SuccessResult>> verificPayPwd(String pwd,String userKey)
    {
        return send(HttpService.getHttpServer().verificPayPwd(pwd, userKey));
    }

    /**
     *  设置 支付密码
     * @param pwd
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> setPayPwd(String pwd,String userKey)
    {
        return send(HttpService.getHttpServer().setPayPwd(pwd, userKey));
    }


    /**
     *  积分支付 完毕
     * 调整订单金额为0的订单为已付款状态   余额支付完 需要调 改接口 把 订单 状态修改为已付款状态
     * @param orderIds
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> setPayOrderByIntegral(String orderIds , String userKey)
    {
        return send(HttpService.getHttpServer().getPayOrderByIntegral(orderIds, userKey));
    }

}
